package com.hippo.core.service;


import com.alibaba.fastjson.JSONObject;
import com.hippo.core.dao.RuleConfigDao;
import com.hippo.core.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: hippo
 */
@Service
public class RuleConfigService {

    @Autowired
    private RuleConfigDao ruleConfigDao;

    public JSONObject getFakerList() {
        List<JSONObject> fakerList = ruleConfigDao.getFakerList();
        ArrayList<Object> list1 = new ArrayList<>();
        ArrayList<Object> list2 = new ArrayList<>();
        ArrayList<Object> list3 = new ArrayList<>();
        ArrayList<Object> list4 = new ArrayList<>();
        ArrayList<Object> list5 = new ArrayList<>();
        ArrayList<Object> list6 = new ArrayList<>();
        for (JSONObject fakerMap : fakerList) {
            HashMap<String, Object> map = new HashMap<>();
            ArrayList<Object> childList = new ArrayList<>();
            if (fakerMap.getString("child_label") != null) {
                HashMap<String, Object> childMap = new HashMap<>();
                childMap.put("label", fakerMap.getString("child_label"));
                childMap.put("value", fakerMap.getString("child_value"));
                childList.add(childMap);
            }
            map.put("value", fakerMap.getString("value"));
            map.put("label", fakerMap.getString("label"));
            if (childList.size() > 0) {
                map.put("children", childList);
            }
            if (fakerMap.getString("type").equals("1")) {
                list1.add(map);
            } else if (fakerMap.getString("type").equals("2")) {
                list2.add(map);
            } else if (fakerMap.getString("type").equals("3")) {
                list3.add(map);
            } else if (fakerMap.getString("type").equals("4")) {
                list4.add(map);
            } else if (fakerMap.getString("type").equals("5")) {
                list5.add(map);
            } else {
                list6.add(map);
            }
        }
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 1; i < 7; i++) {
            HashMap<String, Object> treeMap = new HashMap<>();
            switch (i) {
                case 1:
                    treeMap.put("label", "通用");
                    treeMap.put("value", "1");
                    treeMap.put("children", list1);
                    arrayList.add(treeMap);
                    break;
                case 2:
                    treeMap.put("label", "个人");
                    treeMap.put("value", "2");
                    treeMap.put("children", list2);
                    arrayList.add(treeMap);
                    break;
                case 3:
                    treeMap.put("label", "位置");
                    treeMap.put("value", "3");
                    treeMap.put("children", list3);
                    arrayList.add(treeMap);
                    break;
                case 4:
                    treeMap.put("label", "企业");
                    treeMap.put("value", "4");
                    treeMap.put("children", list4);
                    arrayList.add(treeMap);
                    break;
                case 5:
                    treeMap.put("label", "电脑");
                    treeMap.put("value", "5");
                    treeMap.put("children", list5);
                    arrayList.add(treeMap);
                    break;
                default:
                    treeMap.put("label", "其他");
                    treeMap.put("value", "6");
                    treeMap.put("children", list6);
                    arrayList.add(treeMap);
                    break;
            }
        }
        return CommonUtil.successJson(arrayList);
    }

    public JSONObject reList(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = ruleConfigDao.countRe(jsonObject);
        List<JSONObject> list = ruleConfigDao.reList(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    public JSONObject getParams(String value, String child_value) {
        return CommonUtil.successJson(ruleConfigDao.getParams(value, child_value));

    }

    public JSONObject createRe(JSONObject data) {
        return CommonUtil.response(ruleConfigDao.createRe(data));
    }

    public JSONObject deleteRe(String id) {
        return CommonUtil.response(ruleConfigDao.deleteRe(id));
    }

    public JSONObject updateRe(JSONObject data) {
        return CommonUtil.response(ruleConfigDao.updateRe(data));
    }
}
